# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = 'restructuredtext en'
__all__ = []

from ... import pilelib as stack
from ...gamelib import info as _gi, game
from ...forms import ganjifa as glayout
from ...packlib import info as cs
from ...utillib.misc import kwdefault
from .. import katzenschwanz

GT, SL = _gi.GT, _gi.SL
Wrap = stack.StackWrapper


class Balarama(cs.Dashavatara_Deck, game.Game):
    """
    Finish 1 deck on _foundation_ within a turn.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.ganifa.GhulamLayout

        - row [16 over 2 row N, vert offset]
        - talon [1 S, no offset]
        - reserve [2 vert E, 2 vert W S foundation, no offset]
        - foundation [5 vert E, 5 vert W, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon

        No redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation

        Build up in rank by same suit. Play top. Base ace.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.AC_RowStack (max_cards=12, base_rank=RAJA)

        Init 7 open card per with remain 1st. Build down in rank by alt color.
        Play sequence. Base raja. Max 12 card.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.DefaultHint
    """
    __info__ = dict(id=15414, name='Balarama',
                    category=cs.TYPE_DASHAVATARA_GANJIFA,
                    game_type=(GT.FREECELL | GT.OPEN), decks=1,
                    skill_level=SL.MOSTLY_SKILL, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _layout_ = glayout.GhulamLayout
    _talon_ = stack.InitialDealTalon
    _foundation_ = stack.SS_Foundation
    _row_ = Wrap(stack.AC_RowStack, max_cards=12, base_rank=cs.RAJA)
    _reserve_ = stack.ReserveStack

    def _create_game(self, **kw):
        """
        Create game format.

        Augment pile placements based on current form and group attributes.

        .. \_create_game(rows=16, ncols=8, reserves=4, texts=False,
             playcards=12) -> Layout
        """
        kwdefault(kw, rows=16, ncols=8, reserves=4, texts=False, playcards=12)
        return super(Balarama, self)._create_game(**kw)

    def _start(self, rows=8, **kw):
        """
        Deal initial cards for new game.
          - s[rows]: ~*rows* ea open.

        .. \_start([rows=8]) -> None
        """
        for i in range(rows - 2):
            self.s['talon'].dealRow(frames=0)
        self._play_sound('deal')
        self.s['talon'].dealRow()
        self.s['talon'].dealRowAvail()


class Hayagriva(Balarama):
    """
    Finish 1 deck on _foundation_ within a turn.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.ganifa.GhulamLayout

        - row [16 over 2 row N, vert offset]
        - talon [1 SW, no offset]
        - reserve [2 vert E, 2 vert W S foundation, no offset]
        - foundation [5 vert E, 5 vert W, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon

        No redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation

        Build up in rank by same suit. Play top. Base ace.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.RK_RowStack (max_cards=12, base_rank=RAJA)

        Init 7 open card per with remain 1st. Build down in rank alone. Play
        sequence. Base raja. Max 12 card.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.DefaultHint
    """
    __info__ = dict(id=15415, name='Hayagriva',
                    category=cs.TYPE_DASHAVATARA_GANJIFA,
                    game_type=(GT.ONE_DECK | GT.OPEN), decks=1,
                    skill_level=SL.MOSTLY_SKILL, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _row_ = Wrap(stack.RK_RowStack, max_cards=12, base_rank=cs.RAJA)


class Shanka(Balarama):
    """
    Finish 1 deck on _foundation_ within a turn.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.ganifa.GhulamLayout

        - row [16 over 2 row N, vert offset]
        - talon [1 SW, no offset]
        - foundation [5 vert E, 5 vert W, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon

        No redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation

        Build up in rank by same suit. Play top. Base ace.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.AC_RowStack (max_cards=12, base_rank=RAJA)

        Init 7 open card per with remain 1st. Build down in rank by alt color.
        Play sequence. Base raja. Max 12 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.DefaultHint
    """
    __info__ = dict(id=15416, name='Shanka',
                    category=cs.TYPE_DASHAVATARA_GANJIFA,
                    game_type=(GT.ONE_DECK | GT.OPEN), decks=1,
                    skill_level=SL.MOSTLY_SKILL, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    def _create_game(self, **kw):
        """
        Create game format.

        Augment pile placements based on current form and group attributes.

        .. \_create_game(reserves=0) -> Layout
        """
        kw.setdefault('reserves', 0)
        return super(Shanka, self)._create_game(**kw)


class Surukh(Balarama):
    """
    Finish 1 deck on _foundation_ within a turn.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.ganifa.GhulamLayout

        - row [16 over 2 row N, vert offset]
        - talon [1 SW, no offset]
        - reserve [2 vert E, 2 vert W S foundation, no offset]
        - foundation [5 vert E, 5 vert W, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon

        No redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation

        Build up in rank by same suit. Play top. Base ace.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.AF_RowStack (max_cards=12, base_rank=RAJA)

        Init 7 open card per with remain 1st. Build down in rank by alt force.
        Play sequence. Base raja. Max 12 card.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.DefaultHint
    """
    __info__ = dict(id=15419, name='Surukh',
                    category=cs.TYPE_DASHAVATARA_GANJIFA,
                    game_type=(GT.ONE_DECK | GT.OPEN), decks=1,
                    skill_level=SL.BALANCED, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _row_ = Wrap(stack.AF_RowStack, max_cards=12, base_rank=cs.RAJA)


class Hiranyaksha(cs.Dashavatara_Deck, katzenschwanz.DerKatzenschwanz):
    """
    Finish 1 deck on _foundation_ within a turn.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.CatsTailLayout

        - row [11 horz S reserve, vert offset]
        - talon [1 SW, no offset]
        - reserve [10 N, no offset]
        - foundation [10 over 2 col E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.InitialDealTalon

        No redeals.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation

        Build up in rank by same suit. Play top. Base ace.

      _row_ : pysollib.stack.Stack
        .. pysollib.stack.RK_RowStack (base_rank=GAP)

        Init all cards with raja start pile. Build down in rank alone.
        Play sequence. Base none.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack

        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.games.katzenshwanz.DerKatzenschwanzHint
    """
    __info__ = dict(id=15421, name='Hiranyaksha',
                    category=cs.TYPE_DASHAVATARA_GANJIFA,
                    game_type=(GT.FREECELL | GT.OPEN), decks=1,
                    skill_level=SL.MOSTLY_SKILL, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _row_ = Wrap(stack.RK_RowStack, base_rank=cs.GAP)

    def _create_game(self, **kw):
        """
        Create game format.

        Augment pile placements based on current form and group attributes.

        .. \_create_game(rows=11, reserves=10) -> Layout
        """
        kwdefault(kw, rows=11, reserves=10)
        return super(Hiranyaksha, self)._create_game(**kw)

    def _start(self, *kw):
        """
        Deal initial cards for new game.
          - s[rows]: * open RAJA next.

        .. \_start([]) -> None
        """
        i = 0
        self._play_sound('deal')
        while self.s['talon'].cards:
            if self.s['talon'].TOPCARD['rank'] == cs.RAJA:
                i += int(bool(self.s['rows'][i].cards))
            self.s['talon'].dealRow(rows=[self.s['rows'][i]], frames=4)


PROVIDES = [Balarama, Hayagriva, Hiranyaksha, Shanka, Surukh]